﻿
@using Blazor.FlexGrid.Components
@using Blazor.FlexGrid.Demo.Shared
@using Blazor.FlexGrid.DataAdapters
@using Blazor.Components.Demo.FlexGrid.Services
@using Blazor.FlexGrid.Components.Configuration
@inject WeatherForecastService WeatherService
@inject BlazorComponentColumnCollection<WeatherForecast> Collection
@page "/grid"

<h1>Weather forecast</h1>

@*<BlazorButton>Test</BlazorButton>*@
<GridView DataAdapter="@dataAdapter" PageSize="@pageSize" LazyLoadingOptions="@op"></GridView>
<button class="btn btn-primary" @onclick="@SetPageSize">Increase page size</button>
<button class="btn btn-primary" @onclick="@ReloadCurrentPage">Reload data</button>

@*@{
        RenderFragment<WeatherForecast> weatherTemp = (weather) => @<MatButton Raised="true" Icon="favorite">@weather.Summary</MatButton>;

    Collection.AddColumnValueRenderFunction(w => w.Summary, weatherTemp);
    }*@

@{
    Func<EditColumnContext, RenderFragment<WeatherForecast>> editWeatherSummary = context =>
    {
        RenderFragment<WeatherForecast> editFragment = (weather) => @<input value=@weather.Summary @onchange=@((ChangeEventArgs e) => context.NotifyValueHasChanged(e.Value)) />;


return editFragment;
};
Collection.AddColumnEditValueRenderer(w => w.Summary, editWeatherSummary);

}


@code {
    bool forceReload;
    CollectionTableDataAdapter<WeatherForecast> dataAdapter;

    int pageSize = 5;
    LazyLoadingOptions op;

    protected override async Task OnInitializedAsync()
    {
        var forecast = await WeatherService.GetForecastAsync(DateTime.Now);
        dataAdapter = new CollectionTableDataAdapter<WeatherForecast>(forecast);
    }

    async Task SetPageSize()
    {
        pageSize++;
        var forecast = await WeatherService.GetForecastAsync(new DateTime(2020, 3, 19));
        dataAdapter = new CollectionTableDataAdapter<WeatherForecast>(forecast);
    }

    async Task ReloadCurrentPage()
    {
        await dataAdapter.ReloadCurrentPage();
    }
}
